import { getCode, regUser, regUserLogin, getUInfo, userLogout } from '@/api'
const state = {
    token: localStorage.getItem('token') || '',
    userInfo: {}
}
const mutations = {
    SetUserInfo(state, val) {
        state.userInfo = val
    },
    SetTOKEN(state, val) {
        state.token = val
    }
}
const actions = {
    async getCodeNum({ }, phone) {
        let result = await getCode(phone)
        if (result.code == '200') {
            return result;
        } else {
            return Promise.reject(new Error('请求失败!'))
        }
    },
    async goReg({ }, data) {
        let result = await regUser(data)
        if (result.code == '200') {
            return result;
        } else {
            return Promise.reject(new Error('请求失败!'))
        }
    },
    async loginUser({ commit }, data) {
        let result = await regUserLogin(data)
        if (result.code == '200') {
            commit('SetTOKEN', result.data.token)
            localStorage.setItem('token', result.data.token)
            return result;
        } else {
            return Promise.reject(new Error('请求失败!'))
        }
    },
    async getUserInfo({ commit }) {
        let result = await getUInfo()
        if (result.code == '200') {
            commit('SetUserInfo', result.data)
            return 'ok';
        }
    },
    async logout({ commit }) {
        let result = await userLogout()
        if (result.code == '200') {
            commit('SetUserInfo', {})
            commit('SetTOKEN', '')
            localStorage.removeItem('UUID_TOKEN')
            localStorage.removeItem('token')
            return 'ok';
        }
    }
}
const getters = {}
export default {
    namespaced: true,
    state, mutations, actions, getters
}